package com.hpeng.sys.service.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.hpeng.sys.model.SysUser;
import com.hpeng.sys.service.LoginUserMgr;

@Component("loginUser")
public class LoginUserMgrImpl implements LoginUserMgr {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public String getAccessRight(String userId, String funcCode) {
		String accessRight = "";
		if (userId != null && (!"".equals(userId.trim()))
				&& funcCode != null && (!"".equals(funcCode.trim()))) {
			String sql = "select t.access_right from sys_user_authority t where t.user_code=? and t.func_code=?";
			accessRight = (String) jdbcTemplate.queryForObject(sql, new Object[] {userId, funcCode}, String.class);
		}
		return accessRight;
	}
	
	public String getAccessRight(Map<String, Object> sessionMap, String funcCode) {
		Object o = sessionMap.get("user");
		String userId = "";
		if (o != null) {
			userId = ((SysUser) o).getUserCode();
		}
		String accessRight = "";
		if (userId != null && (!"".equals(userId.trim()))
				&& funcCode != null && (!"".equals(funcCode.trim()))) {
			String sql = "select t.access_right from sys_user_authority t where t.user_code=? and t.func_code=?";
			accessRight = (String) jdbcTemplate.queryForObject(sql, new Object[] {userId, funcCode}, String.class);
		}
		return accessRight;
	}
	
	public String getAccessRight(HttpServletRequest request, String funcCode) {
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String userId = "";
		if (o != null) {
			userId = ((SysUser) o).getUserCode();
		}
		String accessRight = "";
		if (userId != null && (!"".equals(userId.trim()))
				&& funcCode != null && (!"".equals(funcCode.trim()))) {
			String sql = "select t.access_right from sys_user_authority t where t.user_code=? and t.func_code=?";
			accessRight = (String) jdbcTemplate.queryForObject(sql, new Object[] {userId, funcCode}, String.class);
		}
		return accessRight;
	}
	
	//获取登录用户的登录ID
	public String getUserId(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String uCode = "";
		if (o != null) {
			uCode = ((SysUser) o).getUserCode();
		}
		return uCode;
	}
	
	//获取登录用户的登录名
	public String getUserName(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String uName = "";
		if (o != null) {
			uName = ((SysUser) o).getUserName();
		}
		return uName;
	}
	
	//获取登录用户的部门ID
	public String getUserDeptId(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String uDeptId = "";
		if (o != null) {
			uDeptId = ((SysUser) o).getDeptCode();
		}
		return uDeptId;
	}
	
	//获取登录用户的部门名称
	public String getUserDeptName(HttpServletRequest request) {
		String uDeptId = getUserDeptId(request);
		String uDeptName = "";
		if (uDeptId != null && (!"".equals(uDeptId.trim()))) {
			String sql = "select t.depname from department t where t.depid=?";
			uDeptName = (String) jdbcTemplate.queryForObject(sql, new Object[] {uDeptId}, String.class);
		}
		return uDeptName;
	}
	
	//获取登录用户的部门父级ID
	public String getUserDeptParentId(HttpServletRequest request) {
		String uDeptId = getUserDeptId(request);
		String uDeptParentId = "";
		if (uDeptId != null && (!"".equals(uDeptId.trim()))) {
			String sql = "select t.depparentid from department t where t.depid=?";
			uDeptParentId = (String) jdbcTemplate.queryForObject(sql, new Object[] {uDeptId}, String.class);
		}
		return uDeptParentId;
	}
	
	//获取登录用户的部门父级名称
	public String getUserDeptParentName(HttpServletRequest request) {
		String uDeptParentId = getUserDeptParentId(request);
		String uDeptParentName = "";
		if (uDeptParentId != null && (!"".equals(uDeptParentId.trim()))) {
			String sql = "select t.depname from department t where t.depid=?";
			uDeptParentName = (String) jdbcTemplate.queryForObject(sql, new Object[] {uDeptParentId}, String.class);
		}
		return uDeptParentName;
	}
	
	//获取登录用户所关联的员工ID
	public String getUserEmployeeId(HttpServletRequest request){
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String uEmployeeId = "";
		if (o != null) {
			uEmployeeId = ((SysUser) o).getStaid();
		}
		return uEmployeeId;
	}
	
	//获取登录用户所关联的员工姓名
	public String getUserEmployeeName(HttpServletRequest request){
		String uEmployeeId = getUserEmployeeId(request);
		String uEmployeeName = "";
		if (uEmployeeId != null && (!"".equals(uEmployeeId.trim()))) {
			String sql = "select t.staname from staff t where t.staid=?";
			uEmployeeName = (String) jdbcTemplate.queryForObject(sql, new Object[] {uEmployeeId}, String.class);
		}
		return uEmployeeName;
	}
	
	//获取登录用户的功能权限模板代码
	public String getUserTemplateCode(HttpServletRequest request){
		HttpSession session = request.getSession();
		Object o = session.getAttribute("user");
		String templateCode = "";
		if (o != null) {
			templateCode = ((SysUser) o).getTemplateCode();
		}
		return templateCode;
	}
	
	//获取登录用户的功能权限模板名称
	public String getUserTemplateName(HttpServletRequest request){
		String templateCode = getUserTemplateCode(request);
		String templateName = "";
		if (templateCode != null && (!"".equals(templateCode.trim()))) {
			String sql = "select t.template_name from sys_template_mst t where t.template_code=?";
			templateName = (String) jdbcTemplate.queryForObject(sql, new Object[] {templateCode}, String.class);
		}
		return templateName;
	}
	
	//登录人员是否是经办人
	public boolean isJBR(HttpServletRequest request){
		String uEmployeeId = getUserEmployeeId(request);
		String YN = "N";
		if (uEmployeeId != null && (!"".equals(uEmployeeId.trim()))) {
			String sql = "select NVL(t.jbr,'N') jbr from staff t where t.staid=?";
			YN = (String) jdbcTemplate.queryForObject(sql, new Object[] {uEmployeeId}, String.class);
		}
		return ("Y".equals(YN)?true:false);
	}
	
	//登录人员是否是负责人
	public boolean isFZR(HttpServletRequest request){
		String uEmployeeId = getUserEmployeeId(request);
		String YN = "N";
		if (uEmployeeId != null && (!"".equals(uEmployeeId.trim()))) {
			String sql = "select NVL(t.fzr,'N') fzr from staff t where t.staid=?";
			YN = (String) jdbcTemplate.queryForObject(sql, new Object[] {uEmployeeId}, String.class);
		}
		return ("Y".equals(YN)?true:false);
	}
	
}